﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Toolkit.Web.Helpers;
using Toolkit.Web.ViewComponents.DataGrid;

namespace Toolkit.Web.Mvc3.Examples.Controllers
{
    public class DataGridController : Controller
    {
        private const string _persistenceKey = "datagridsettings-mvc3";

        public ActionResult Index(DataGridInputModel input, bool delay = false)
        {
            DataGridModel<GridEntry> model = GetModel(input);

            if (delay)
            {
                model.DataResults = null;
            }

            //model.DataResults = new List<GridEntry>();
            //model.TotalNumberOfResults = 0;

            return View(model);
        }

        public ActionResult Index_Ajax(DataGridInputModel input)
        {
            DataGridModel<GridEntry> model = GetModel(input);

            //model.DataResults = new List<GridEntry>();
            //model.TotalNumberOfResults = 0;

            return View(model);
        }

        public ActionResult SavePreferences_Ajax(PersistentDataGridModel input, string key)
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException("key");
            }

            HttpCookie cookie = new HttpCookie(key);
            cookie.Value = JsonSerialization.Serialize<PersistentDataGridModel>(input);
            cookie.Expires = DateTime.Now.AddDays(1);
            Response.Cookies.Add(cookie);

            return new EmptyResult();
        }

        private DataGridModel<GridEntry> GetModel(DataGridInputModel input)
        {
            // Note: 'input' is never null due to mvc's defualt model binder.
            input.ApplyPersistentSettings(Request.Cookies[_persistenceKey]);

            // Default record set size if not set.
            if (!input.RecordSetSize.HasValue)
            {
                input.RecordSetSize = 500;
            }

            // Clean filters.
            input.CleanModel();

            int total = (500 * 9) + 2;
            List<GridEntry> entries = new List<GridEntry>();

            for (int i = input.RecordStartIndex; i < Math.Min((input.RecordStartIndex + input.RecordSetSize.Value), total); i++)
            {
                entries.Add(new GridEntry(i));
            }
            
            DataGridModel<GridEntry> model = new DataGridModel<GridEntry>();
            model.DataResults = entries;
            model.InputModel = input;
            model.TotalNumberOfResults = total;
            model.PersistenceKey = _persistenceKey;

            return model;
        }

        public class GridEntry
        {
            public GridEntry(int index)
            {
                Column0 = string.Format("Row {0} Column 0", index);
                Column1 = string.Format("Row {0} Column 1", index);
                Column2 = string.Format("Row {0} Column 2", index);
                Column3 = string.Format("Row {0} Column 3", index);
                Column4 = string.Format("Row {0} Column 4", index);
                Column5 = string.Format("Row {0} Column 5", index);
                Column6 = string.Format("Row {0} Column 6", index);
                Column7 = string.Format("Row {0} Column 7", index);
                Column8 = string.Format("Row {0} Column 8", index);
                Column9 = string.Format("Row {0} Column 9", index);
                Column10 = string.Format("Row {0} Column 10", index);
                Column11 = string.Format("Row {0} Column 11", index);
                Column12 = string.Format("Row {0} Column 12", index);
                Column13 = string.Format("Row {0} Column 13", index);
                Column14 = string.Format("Row {0} Column 14", index);
                Column15 = string.Format("Row {0} Column 15", index);
                Column16 = string.Format("Row {0} Column 16", index);
                Column17 = string.Format("Row {0} Column 17", index);
                Column18 = string.Format("Row {0} Column 18", index);
                Column19 = string.Format("Row {0} Column 19", index);
                Column20 = string.Format("Row {0} Column 20", index);
                Column21 = string.Format("Row {0} Column 21", index);
                Column22 = string.Format("Row {0} Column 22", index);
                Column23 = string.Format("Row {0} Column 23", index);
                Column24 = string.Format("Row {0} Column 24", index);
                Column25 = string.Format("Row {0} Column 25", index);
                Column26 = string.Format("Row {0} Column 26", index);
                Column27 = string.Format("Row {0} Column 27", index);
                Column28 = string.Format("Row {0} Column 28", index);
                Column29 = string.Format("Row {0} Column 29", index);
                Column30 = string.Format("Row {0} Column 30", index);
                Column31 = string.Format("Row {0} Column 31", index);
                Column32 = string.Format("Row {0} Column 32", index);
                Column33 = string.Format("Row {0} Column 33", index);
                Column34 = string.Format("Row {0} Column 34", index);
                Column35 = string.Format("Row {0} Column 35", index);
                Column36 = string.Format("Row {0} Column 36", index);
                Column37 = string.Format("Row {0} Column 37", index);
                Column38 = string.Format("Row {0} Column 38", index);
                Column39 = string.Format("Row {0} Column 39", index);
                Column40 = string.Format("Row {0} Column 40", index);
                Column41 = string.Format("Row {0} Column 41", index);
                Column42 = string.Format("Row {0} Column 42", index);
                Column43 = string.Format("Row {0} Column 43", index);
                Column44 = string.Format("Row {0} Column 44", index);
                Column45 = string.Format("Row {0} Column 45", index);
                Column46 = string.Format("Row {0} Column 46", index);
                Column47 = string.Format("Row {0} Column 47", index);
                Column48 = string.Format("Row {0} Column 48", index);
                Column49 = string.Format("Row {0} Column 49", index);
            }

            public string Column0
            {
                get;
                set;
            }

            public string Column1
            {
                get;
                set;
            }

            public string Column2
            {
                get;
                set;
            }

            public string Column3
            {
                get;
                set;
            }

            public string Column4
            {
                get;
                set;
            }

            public string Column5
            {
                get;
                set;
            }

            public string Column6
            {
                get;
                set;
            }

            public string Column7
            {
                get;
                set;
            }

            public string Column8
            {
                get;
                set;
            }

            public string Column9
            {
                get;
                set;
            }

            public string Column10
            {
                get;
                set;
            }

            public string Column11
            {
                get;
                set;
            }

            public string Column12
            {
                get;
                set;
            }

            public string Column13
            {
                get;
                set;
            }

            public string Column14
            {
                get;
                set;
            }

            public string Column15
            {
                get;
                set;
            }

            public string Column16
            {
                get;
                set;
            }

            public string Column17
            {
                get;
                set;
            }

            public string Column18
            {
                get;
                set;
            }

            public string Column19
            {
                get;
                set;
            }

            public string Column20
            {
                get;
                set;
            }

            public string Column21
            {
                get;
                set;
            }

            public string Column22
            {
                get;
                set;
            }

            public string Column23
            {
                get;
                set;
            }

            public string Column24
            {
                get;
                set;
            }

            public string Column25
            {
                get;
                set;
            }

            public string Column26
            {
                get;
                set;
            }

            public string Column27
            {
                get;
                set;
            }

            public string Column28
            {
                get;
                set;
            }

            public string Column29
            {
                get;
                set;
            }

            public string Column30
            {
                get;
                set;
            }

            public string Column31
            {
                get;
                set;
            }

            public string Column32
            {
                get;
                set;
            }

            public string Column33
            {
                get;
                set;
            }

            public string Column34
            {
                get;
                set;
            }

            public string Column35
            {
                get;
                set;
            }

            public string Column36
            {
                get;
                set;
            }

            public string Column37
            {
                get;
                set;
            }

            public string Column38
            {
                get;
                set;
            }

            public string Column39
            {
                get;
                set;
            }

            public string Column40
            {
                get;
                set;
            }

            public string Column41
            {
                get;
                set;
            }

            public string Column42
            {
                get;
                set;
            }

            public string Column43
            {
                get;
                set;
            }

            public string Column44
            {
                get;
                set;
            }

            public string Column45
            {
                get;
                set;
            }

            public string Column46
            {
                get;
                set;
            }

            public string Column47
            {
                get;
                set;
            }

            public string Column48
            {
                get;
                set;
            }

            public string Column49
            {
                get;
                set;
            }
        }
    }
}
